<template>
  <div class="goods">
    <bread-crumb
      :path="{ path: '/home' }"
      :titles="['首页', '商品管理', '商品列表']"
    ></bread-crumb>
    <el-card>
      <!-- 卡片顶部区 -->
      <el-row :gutter="20">
        <el-col :span="8">
          <!-- 这里的v-model绑定的是query数据 这个数据和后台的查询有关 后台根据这个query 返回相应的数据 -->
          <el-input
            placeholder="请输入商品名称"
            v-model="goodsListParams.query"
            clearable
          >
            <el-button
              slot="append"
              icon="el-icon-search"
              @click="getGoodsList"
            ></el-button>
          </el-input>
        </el-col>
        <el-col :span="4">
          <el-button type="primary" @click="addGood">添加商品</el-button>
        </el-col>
      </el-row>
      <!-- 卡片表格区域 -->
      <goods-table
        :tableData="TableData"
        :TableLabelsAndData="TableLabelsAndData"
        @removeGood="removeGood"
      ></goods-table>
      <!-- 分页区域 -->
      <el-pagination
        @size-change="sizeChange"
        @current-change="currentChange"
        :current-page="goodsListParams.pagenum"
        :page-sizes="[5, 10]"
        :page-size="goodsListParams.pagesize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="totalTableDataNum"
      ></el-pagination>
    </el-card>
  </div>
</template>

<script>
import GoodsTable from "./goodsChildren/GoodsTable";
import BreadCrumb from "@/components/BreadCrumb";
export default {
  name: "Goods",
  data() {
    return {
      //请求数据的参数
      goodsListParams: {
        query: "",
        pagenum: 1,
        pagesize: 10
      },
      totalTableDataNum: 0,
      TableData: [],
      TableRules: {},
      TableLabelsAndData: {
        labels: ["商品名称", "商品价格", "商品重量"],
        datas: ["goods_name", "goods_price", "goods_weight"]
      }
    };
  },
  created() {
    this.getGoodsList();
  },
  methods: {
    getGoodsList() {
      this.$http
        .get("goods", {
          params: this.goodsListParams
        })
        .then(res => {
          if (res.data.meta.status == 200) {
            this.totalTableDataNum = res.data.data.total;
            this.TableData = res.data.data.goods;
            console.log(this.TableData);
          } else {
            this.$message.error("获取表格数据失败");
          }
        })
        .catch(err => err);
    },
    sizeChange(newSize) {
      (this.goodsListParams.pagesize = newSize), this.getGoodsList();
    },
    currentChange(newPage) {
      this.goodsListParams.pagenum = newPage;
      this.getGoodsList();
    },
    removeGood(id) {
      this.$confirm("确认删除该商品吗", "提示", {
        confirmButtonText: "删除",
        CancelButtonText: "取消",
        type: "warning"
      })
        .then(res => {
          if (res == "confirm") {
            this.$http
              .delete(`goods/${id}`)
              .then(res => {
                if (res.data.meta.status == 200) {
                  this.$message.success("删除成功");
                  this.getGoodsList();
                } else {
                  this.$message.error("删除失败");
                }
              })
              .catch(err => err);
          } else {
            console.log("1111");
          }
        })
        .catch(() => {
          this.$message.error("已取消删除");
        });
    },
    addGood() {
      this.$router.push("/home/goods/add");
    }
  },
  components: {
    BreadCrumb,
    GoodsTable
  }
};
</script>

<style lang="scss" scoped></style>
